package procsim;

import java.awt.*;
import java.awt.event.*;

public class ClickableElement implements GraphicalElement, MouseListener{
    private int x, y, width, height;
    private int tabindex;
    private String name;
    private boolean bottom;
    public ClickableElement(int tabindex, String name){
        this.tabindex=tabindex;
        this.name=name;
    }
    public ClickableElement(int tabindex, String name, int width, int height){
        this(tabindex, name);
        this.width=width;
        this.height=height;
    }
    public void setBottom(boolean bottom){
        this.bottom=bottom;
    }
    @Override
    public void setCoords(int x, int y) {
        this.x=x;
        this.y=y;
    }
    @Override
    public int getWidth() {
        return width;
    }
    @Override
    public int getHeight() {
        return height;
    }
    @Override
    public void setSize(int width, int height) {
        this.width=width;
        this.height=height;
    }
    @Override
    public void drawIt(Graphics2D g, int x, int y) {
        g.drawRect(x, y, width, height);
        Font f = g.getFont();
        g.setFont(new Font("Arial", Font.PLAIN, 16));
        if(bottom)
            g.drawString(name, x+width/2-(int)(name.length()*3.5), y+height-7);
        else
            g.drawString(name, x+width/2-(int)(name.length()*3.5), y+height/2+8);
        g.setFont(f);
    }
    @Override
    public void drawIt(Graphics2D g) {
        drawIt(g, x, y);
    }
    @Override
    public ClickableElement result() {
        return this; // does nothing
    }
    @Override
    public ClickableElement tick() {
        return this; // does nothing
    }
    @Override
    public void updateSignal(Sig sig) { }
    @Override
    public void mouseClicked(MouseEvent e) { }
    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getX()>x && e.getX()<x+width && e.getY()>y && e.getY()<y+height){
            if(e.getComponent() instanceof PaintTab)
                ((PaintTab)e.getComponent()).changeTab(tabindex);
        }
    }
    @Override
    public void mouseReleased(MouseEvent e) { }
    @Override
    public void mouseEntered(MouseEvent e) { }
    @Override
    public void mouseExited(MouseEvent e) { }
}
